package com.mystore.dao.impl;

import com.mystore.dao.AddressDao;
import com.mystore.dao.BaseDao;
import com.mystore.entity.Address;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddressDaoImpl implements AddressDao {
    private BaseDao baseDao;

    public AddressDaoImpl() {
        this.baseDao =new BaseDaoImpl();
    }

    @Override
    public List<Address> findAllMyAddressByUid(int uid, Connection connection) {
        String sql = "select * from address  where uid=?";
        List<Object> list = new ArrayList<>();
        list.add(uid);
        List<Address> addressList = null;
        try {
            addressList= baseDao.findMoreRefResult(sql,list,Address.class,connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return addressList;
    }

    @Override
    public Address findAddressByAidAndUid(int id, int uid, Connection connection) {
        String sql = "select * from address  where id=? and uid=?";
        List<Object> list = new ArrayList<>();
        list.add(id);
        list.add(uid);
        Address address = null;
        try {
            address = (Address) baseDao.findSingleResult(sql,list,Address.class,connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return address;
    }

    @Override
    public Boolean save(Address address, Connection connection) throws SQLException {
        String sql = "insert into  address(uid,address,tel)  values(?,?,?)";
        List<Object> list = new ArrayList<>();
        list.add(address.getUid());
        list.add(address.getAddress());
        list.add(address.getTel());
        return baseDao.updateByPreparedStatement(sql,list,connection);
    }
}
